// deltaq
// (c) 2010, Normen Müller, normen.mueller@gmail.com  
// $Id$
package deltaq.svn

import java.text.MessageFormat

import org.tmatesoft.svn.core.SVNException
import org.tmatesoft.svn.cli.AbstractSVNCommand

import scaup.data.Conversions._

class SVNHelpCommand extends org.tmatesoft.svn.cli.svn.SVNHelpCommand {
  private val GENERIC_HELP_HEADER =
    "usage: {0} <subcommand> [options] [args]\n" +
    "Subversion command-line client, version {1}.\n" +
    "Type ''{0} help <subcommand>'' for help on a specific subcommand.\n" +
    "Type ''{0} --version'' to see the program version and RA modules\n" +
    "  or ''{0} --version --quiet'' to see just the version number.\n" +
    "\n" +
    "Most subcommands take file and/or directory arguments, recursing\n" +
    "on the directories.  If no arguments are supplied to such a\n" +
    "command, it recurses on the current directory (inclusive) by default.\n" +
    "\n" +
    "Available subcommands:"
  
  private val GENERIC_HELP_FOOTER =
    "DeltaQ is a Java/Scala version of Subversion - a tool for version control.\n" +
    "For additional information, see http://code.google.com/p/deltaq/\n"
  
  private val VERSION_HELP_FOOTER =
    "\nThe following repository access (RA) modules are available:\n\n" +
    "* org.tmatesoft.svn.core.internal.io.dav : Module for accessing a repository via WebDAV protocol.\n" +
    "  - handles 'http' scheme\n" +
    "  - handles 'https' scheme\n" +
    "* org.tmatesoft.svn.core.internal.io.svn: Module for accessing a repository using the svn network protocol.\n" + 
    "  - handles 'svn' scheme\n" +
    "* org.tmatesoft.svn.core.internal.io.fs: Module for accessing a repository on local disk.\n" +
    "  - handles 'file' scheme (only FSFS repositories are supported)\n"

    @throws(classOf[SVNException])
    override def run() {
      if (!getSVNEnvironment.getArguments.isEmpty()) {
        for (commandName <- getSVNEnvironment.getArguments().iterator.asInstanceOf[java.util.Iterator[String]]) {          
          val command = AbstractSVNCommand.getCommand(commandName)
          if (command == null)
            getSVNEnvironment.getErr.println("\"" + commandName + "\": unknown command.\n")
          else {
        	val help = org.tmatesoft.svn.cli.SVNCommandUtil.getCommandHelp(command, getEnvironment.getProgramName, true)
        	getSVNEnvironment.getOut.println(help)
        	getSVNEnvironment.getOut.println()
          }
        }
      } else if (getSVNEnvironment.isVersion) {
        val version = SVNCommandUtil.getVersion(getEnvironment, getSVNEnvironment.isQuiet)
        getEnvironment.getOut.println(version)
        if (!getSVNEnvironment.isQuiet)
          getEnvironment().getOut().println(VERSION_HELP_FOOTER)        
      } else if (getSVNEnvironment.getArguments.isEmpty) {
        val help = SVNCommandUtil.getGenericHelp(getEnvironment.getProgramName, GENERIC_HELP_HEADER, GENERIC_HELP_FOOTER, null)
        getSVNEnvironment.getOut.print(help)
      } else {
        val message = MessageFormat.format("Type ''{0} help'' for usage.", Array[Object] {getSVNEnvironment.getProgramName}: _*)
        getSVNEnvironment.getOut.println(message)
      }
    }
}
